<?php
	/**
	 * 
	 * Product model
	 * @author Trung
	 *
	 */
	class ProductModel extends Model
	{
		/**
		 * Constructor
		 */
		function __construct()
		{
			parent::__construct();
		}
		
		function check_product($productId){
            $sql = "select * from product where id = ".$productId;
            $res = $this->execute_query($sql);
            if(count($res) != 0){
                return true;
            }
            return false;
        }
         
		/**
         * 
         * Gets all record from table Product
         */
        function getAllProductByCategory($id){
            $sql = 'SELECT p.*, cp.category_id 
			FROM product AS p 
			INNER JOIN category_product AS cp ON p.id = cp.product_id
			INNER JOIN store As s ON s.id = p.store_id
			WHERE s.status = 1 and cp.category_id = ?';
            
            $values = array();
            $values[0] = $id;
            $types = array();
            $types[0] = "integer";
            $res = $this->execute_query($sql, $values, $types);
            
            if(is_array($res) && count($res) > 0){
                return $res;
            }
            return null;
        }
        function getAllProductBySearch($keyWord){
            $sql = "select p.* 
			from product as p
			inner join store as s on s.id = p.store_id
			where s.status = 1 and (p.name like '%" . $keyWord ."%')";
            return $this->execute_query($sql);
        }
        /**
         * 
         * Get products from premium shop
         */
        function getFeatureProducts(){
            //$sql = 'SELECT p.* FROM product AS p INNER JOIN category_product AS cp ON p.id = cp.product_id WHERE cp.category_id = ?';
            $sql = 'SELECT p.* FROM product AS p, store AS s WHERE s.status = 1 and s.premium = 1 and p.store_id = s.id';
            $res = $this->execute_query($sql);
            
            if(is_array($res) && count($res) > 0){
                return $res;
            }
            return null;
        }
        
        /**
         * 
         * Get products on sale
         */
        function getSalesProducts(){
        	$sql = "select p.* 
			from product p 
			inner join price pr on p.id = pr.product_id
			inner join store s on s.id = p.store_id
			where s.status = 1 and pr.discount > 0";
        	$res = $this->execute_query($sql);
        	
        	if(is_array($res) && count($res) > 0){
                return $res;
            }
            return null;
        }
        
        /**
         * 
         * Gets a specific product by id
         * @param string $id
         */
        function getProductById($id){
            //$sql = "select p.*,s.name as storename,pr.start_date,pr.end_date,pr.discount,pr.price from product as p, store as s, price as pr where pr.product_id = p.id and p.store_id = s.id and p.publish = 1 and p.id = ?";
            $sql = "select p.*,s.name as storename,s.phone,pr.start_date,pr.end_date,pr.discount,pr.price,c.name As catgname from product p 
            inner join category_product cp on p.id = cp.product_id
            inner join category c on c.id = cp.category_id
            inner join price pr on pr.product_id = p.id  
            inner join store s on p.store_id = s.id
            where p.publish = 1 and p.id = ?";
            $values = array();
            $values[0] = $id;
            $types = array();
            $types[0] = "integer";
            
            $res = $this->execute_query($sql, $values, $types);
            if(is_array($res) && count($res) > 0){
                return $res[0];
            }
            return null;
        }
        
        /**
         * 
         * Gets products query
         * @param string $query
         */
        function getProductsByQuery($query){
        	$sql = "select p.*, p.id as fav from product p, store s where s.status = 1 and p.store_id = s.id " . $query;
			$res = $this->execute_query($sql);
        	
        	if(is_array($res) && count($res) > 0){
                return $res;
            }
            return null;
        }
        
        /**
         * 
         * Gets products for specific product name
         * @param string $keyWord
         */
         function getAllProductSearchByName($keyWord){
            $sql = "select p.* from product as p inner join store as s on s.id = p.store_id where s.status = 1 and (p.name like '%" . $keyWord ."%')";
            return $this->execute_query($sql);
        }
        /**
         * 
         * Gets product for specific brand name
         * @param string $keyWord
         */
        function getAllProductSearchByBrand($keyWord){
            $sql = "select p.* from product p
			inner join brand b on p.brand_id = b.id
			inner join store as s on s.id = p.store_id
			where s.status = 1 and (b.name like '%" . $keyWord ."%')";
            return $this->execute_query($sql);
        }
        
        /**
         * 
         * Gets products for specific store name
         * @param string $keyWord
         */
        function getAllProductSearchByStore($keyWord){
            $sql = "select p.*
			from product p
			inner join store s on p.store_id = s.id
			where s.status = 1 and (s.name like '%" . $keyWord ."%')"; 
            return $this->execute_query($sql);
        }
        
        /**
         * 
         * Get products from premium shop
         */
        function getAllProductName(){
             $sql="select name from product limit 0,4";
             $result = $this->execute_query($sql);
             $data = '';
             foreach($result As $row){
                 $data .= $row['name'] . ",";
             }
             return $data;
        }
        /**
         * 
         * Gets all brand
         */
        function getAllBrand(){
             $sql="select name from brand limit 0,6";
             $result = $this->execute_query($sql);
             $data = '';
             foreach($result As $row){
                 $data .= $row['name'] . ",";
             }
             return $data;
        }
        
        /**
         * 
         * Gets all store
         */
        function getAllStore(){
             $sql="select name from store limit 0,6";
             $result = $this->execute_query($sql);
             $data = '';
             foreach($result As $row){
                 $data .= $row['name'] . ",";
             }
             return $data;
        } 
        
        /**
         * 
         * Gets favorite product by user id
         * @param integer userId
         */
        function getFavProductsByUserId($userId){
        	$sql = "select p.*
        	from product as p
        	inner join user_product as up on p.id = up.product_id
        	inner join store as s on p.store_id = s.id
        	where s.status = 1 and p.publish =  1 and up.user_id = ".$userId;
        	$res = $this->execute_query($sql);
        	if(is_array($res) && count($res) > 0){
                return $res;
            }
            return array();
        }
	}
?>